package com.example.yiparkapplication.activity.index.activity.myCar;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.yiparkapplication.R;
import com.example.yiparkapplication.activity.IndexActivity;

public class CarListAdapter extends RecyclerView.Adapter<CarListAdapter.ViewHolder> {

    private String[] mCarNumberArr;
    private IndexActivity mIndexActivity;

    static class ViewHolder extends RecyclerView.ViewHolder{
        View carListView;
        TextView carNumber;

        public ViewHolder(View view){
            super(view);
            carListView = view;
            carNumber = view.findViewById(R.id.id_license_plate);
        }
    }
//    类初始化时，要穿进来的参数
    public CarListAdapter(String[] carNumberList, IndexActivity indexActivity){
        mCarNumberArr = carNumberList;
        mIndexActivity = indexActivity;
    }

    //对，生成的列表item元素添加事件响应
    @Override
    public CarListAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType){
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_car_list_layout, parent, false);
        final CarListAdapter.ViewHolder holder = new CarListAdapter.ViewHolder(view);

        holder.carListView.findViewById(R.id.id_car_order).setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                int position = holder.getAdapterPosition();
                String licensePlate = mCarNumberArr[position];
                mIndexActivity.toCarOrderNopayActivity(licensePlate);
            }
        });

        holder.carListView.findViewById(R.id.id_more).setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                int position = holder.getAdapterPosition();
                String licensePlate = mCarNumberArr[position];
                mIndexActivity.toCarMoreActivity(licensePlate);
            }
        });
        return holder;
    }

    //给item元素赋值
    @Override
    public void onBindViewHolder(@NonNull CarListAdapter.ViewHolder holder, int position){
        String licensePlate = mCarNumberArr[position];
        holder.carNumber.setText(licensePlate);
    }

    @Override
    public int getItemCount(){
        return mCarNumberArr.length;
    }
}
